package service

import (
	"errors"
	"github.com/gccai/write_helper/config"
	"github.com/gccai/write_helper/model"
	"github.com/gccai/write_helper/query"
	"github.com/gccai/write_helper/repository"
)

type BookSrvInterface interface {
	Add(book *model.Book) (*model.Book, error)
	Delete(book *model.Book) (bool, error)
	Edit(book *model.Book) (bool, error)
	GetOneBook(book *model.Book) (*model.Book, error)
	GetTotal(userid string) (int, error)
	BookList(req *query.ListQuery, userId string) ([]*model.Book, error)
	ExistByBookName(userId, bookName string) *model.Book
	ExistAndDeleted(book *model.Book) *model.Book
	//GetChapterCount(book *model.Book) (int64, error)
	UpdateChapter(book *model.Book) bool
}

type BookService struct {
	Repo repository.BookRepositoryInterface
}

func (b BookService) Add(book *model.Book) (*model.Book, error) {
	if exist := b.Repo.ExistByBookName(book.UserId,book.BookName); exist != nil {
		if exist2 := b.Repo.ExistAndDeleted(book); exist2 != nil {
			book.IsDeleted = false
			res, err := b.Repo.Edit(book)
			if !res {
				return nil, err
			}
			return book, nil
		}
		return nil, errors.New("书籍存在，请更换书名")
	}
	return b.Repo.Add(book)
}

func (b BookService) Delete(book *model.Book) (bool, error) {
	if book.BookName == "" || book.UserId == "" {
		return false, errors.New("参数错误")
	}
	if b.Repo.ExistAndDeleted(book) != nil {
		return b.Repo.Delete(book.BookName, book.UserId)
	}
	return false, errors.New("书籍不存在")
}

func (b BookService) Edit(book *model.Book) (bool, error) {
	if book.UserId == "" {
		return false, errors.New("service层检测到userid为空")
	}
	return b.Repo.Edit(book)
}

func (b BookService) GetOneBook(book *model.Book) (*model.Book, error) {
	if book.BookName == "" || book.UserId == ""{
		return nil, errors.New("service层检测到bookname为空")
	}
	exist := b.ExistAndDeleted(book)
	if exist == nil {
		return nil, errors.New("查无此书")
	}
	return b.Repo.GetOneBook(book)
}

func (b BookService) GetTotal(userId string) (int, error) {
	if userId == "" {
		return 0, errors.New("service层检测到作者名子是空哦")
	}
	exist := b.Repo.ExistByUserId(userId)
	if exist == nil {
		return 0, errors.New("该作者不存在哦")
	}
	var total int = 0
	for _, _ = range exist {
		total++
	}
	return total, nil
}

func (b BookService) BookList(req *query.ListQuery, userId string) ([]*model.Book, error) {
	if req.PageSize < 10 {
		req.PageSize = config.PAGE_SIZE
	}
	if userId == "" {
		return nil, errors.New("service层检测到，作者姓名是空")
	}
	if exist := b.Repo.ExistByUserId(userId); exist == nil {
		return nil, errors.New("作者不存在，请检查作者姓名重新查找。")
	}
	return b.Repo.BookList(req, userId)
}

func (b BookService) ExistByBookName(userId, bookName string) *model.Book {
	if bookName == "" || userId == ""{
		return nil
	}
	return b.Repo.ExistByBookName(userId, bookName)
}

func (b BookService) ExistAndDeleted(book *model.Book) *model.Book {
	if book.BookName == "" || book.UserId == ""{
		return nil
	}
	return b.Repo.ExistAndDeleted(book)
}

func (b BookService) UpdateChapter(book *model.Book) bool {
	isSuccess, err := b.Repo.UpdateChapter(book)
	if err != nil {
		return isSuccess
	}
	return isSuccess
}

//func (b BookService) GetChapterCount(book *model.Book) (int64, error) {
//	if book.UserId == "" || book.BookName == "" {
//		return -1, errors.New("service层检测到空值")
//	}
//	exist := b.ExistByBookName(book.BookName)
//	if exist == nil {
//		return -1, errors.New("该书不存在。")
//	}
//	return b.Repo.GetChapterCount(book)
//}
